home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 14.9 KB | 564 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: EmbedRun.cpp
- // Release Version: $ 1.0d1 $
- //
- // Author: Anthone Burbidge
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef _EMBEDRUN_
- #include "EmbedRun.h"
- #endif
-
- #ifndef _TEXTPART_
- #include "TextPart.h"
- #endif
-
- #ifndef _TEXTFACET_
- #include "TextFacet.h"
- #endif
-
- #ifndef _TEXTFRAME_
- #include "TextFrame.h"
- #endif
-
- #ifndef _TEXTPROXY_
- #include "TextProxy.h"
- #endif
-
- #ifndef _VALUESTREAM_
- #include "ValueStream.h"
- #endif
-
- #ifndef _TEXTSELC_
- #include "TextSelc.h"
- #endif
-
- #ifndef _ODTEXT_
- #include "ODText.h"
- #endif
-
- // ----- Textension Includes -----
-
- #ifndef _Frames_
- #include "Frames.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _INFO_
- #include <Info.h>
- #endif
-
- #ifndef _CANVAS_
- #include <Canvas.h>
- #endif
-
- #ifndef _FACET_
- #include <Facet.h>
- #endif
-
- #ifndef _FRAME_
- #include <Frame.h>
- #endif
-
- #ifndef _PART_
- #include <Part.h>
- #endif
-
- #ifndef _SHAPE_
- #include <Shape.h>
- #endif
-
- #ifndef _STORAGEU_
- #include <StorageU.h>
- #endif
-
- #ifndef _DRAFT_
- #include <Draft.h>
- #endif
-
- #ifndef _XMPSESSM_
- #include <XMPSessM.h>
- #endif
-
- #ifndef _WINDOW_
- #include <Window.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #ifndef __FIXMATH__
- #include <FixMath.h>
- #endif
-
- #ifndef mathRoutinesIncludes
- #include <Math Routines.h>
- #endif
-
- #ifndef __DRAG__
- #include <Drag.h>
- #endif
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #pragma segment TextPartSegment
-
- //========================================================================================
- // CLASS CEmbeddedRun
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::CEmbeddedRun
- //----------------------------------------------------------------------------------------
-
- CEmbeddedRun::CEmbeddedRun() :
- fProxyRun(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::InitEmbeddedRun
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::InitEmbeddedRun()
- {
- InitBetterGraphicsRun(kSelectionWidth);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::~CEmbeddedRun
- //----------------------------------------------------------------------------------------
-
- CEmbeddedRun::~CEmbeddedRun()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::CreateNew
- //----------------------------------------------------------------------------------------
-
- CAttrObject* CEmbeddedRun::CreateNew() const
- {
- CEmbeddedRun* attrObject = new CEmbeddedRun();
- if (attrObject)
- attrObject->InitEmbeddedRun();
-
- return attrObject;
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::GetClassId
- //----------------------------------------------------------------------------------------
-
- ClassId CEmbeddedRun::GetClassId() const
- {
- return kEmbeddedPartClassId;
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::GetAttrIDs
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::GetAttrIDs(short* countAttr, AttrId* attrArray) const
- {
- CBetterGraphicsRun::GetAttrIDs(countAttr, attrArray);
-
- attrArray[(*countAttr)++] = kProxyRunAttr;
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::FreeData
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::FreeData()
- {
- short svOrgX = XMPASLMQDGlobals.thePort->portRect.left;
- short svOrgY = XMPASLMQDGlobals.thePort->portRect.top;
-
- BC_TDynamicCollection<XMPFrame*, BC_CUnmanaged> tempCollect(4);
-
- BC_TCollectionActiveIterator<FW_CProxyFrame *> ite(*fProxyRun->GetProxyFrameList());
- for (; !ite.IsDone(); ite.Next())
- {
- FW_CProxyFrame *frame = *ite.CurrentItem();
- if (frame->GetContainingFrame() != NULL)
- tempCollect.Append(frame->GetXMPFrame());
- }
-
- BC_TCollectionActiveIterator<XMPFrame *> ite2(tempCollect);
- for (; !ite2.IsDone(); ite2.Next())
- {
- fProxyRun->GetEmbeddingPart()->RemoveEmbeddedFrame(*ite2.CurrentItem());
- }
-
- ::SetOrigin(svOrgX, svOrgY);
-
- // Should delete fProxyRun is empty
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::WriteToStream
- //----------------------------------------------------------------------------------------
-
- OSErr CEmbeddedRun::WriteToStream(CStream* outputStream)
- {
- CTextPart* textPart = (CTextPart *) fProxyRun->GetEmbeddingPart();
- CTextSelection* selection = (CTextSelection *) textPart->GetSelection();
- XMPStorageUnit* storage = ((CValueStream *) outputStream)->GetStorageUnit();
-
- OSErr err = CBetterGraphicsRun::WriteToStream(outputStream);
-
- if (selection->IsCloning())
- fProxyRun->CloneTo(storage, selection->GetCommandFrame(),
- selection->GetCloneKind());
- else
- fProxyRun->Externalize(storage);
-
- Rect boundingBox;
- fProxyRun->GetBoundingBox(boundingBox);
- outputStream->WriteRect(&boundingBox);
-
- return noErr;
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::ReadFromStream
- //----------------------------------------------------------------------------------------
-
- OSErr CEmbeddedRun::ReadFromStream(CStream* inputStream)
- {
- CTextPart* textPart = CTextPart::GetCurrentPart();
- CTextSelection* selection = (CTextSelection *) textPart->GetSelection();
- XMPStorageUnit* storage = ((CValueStream *) inputStream)->GetStorageUnit();
-
- OSErr err = CBetterGraphicsRun::ReadFromStream(inputStream);
-
- XMPFrame* embeddedXMPFrame;
-
- fProxyRun = (CTextProxyRun *) textPart->NewProxyRun();
-
- if (selection->IsCloning())
- embeddedXMPFrame = fProxyRun->CloneFrom(storage, selection->GetCloneKind());
- else
- fProxyRun->Internalize(storage);
-
- Rect boundingBox;
- inputStream->ReadRect(&boundingBox);
- fProxyRun->SetBoundingBox(boundingBox);
-
- if (selection->IsCloning())
- {
- // ----- Embed the part -----
- Point ptTemp;
- ::SetPt(&ptTemp, boundingBox.left, boundingBox.top);
- XMPTransform* externalXForm = ::NewXMPTransform(ptTemp);
-
- textPart->EmbedFrame(embeddedXMPFrame,
- fProxyRun,
- textPart->GetMainPresentation(),
- externalXForm,
- FALSE);
- }
-
- return noErr;
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::DrawSelection
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::DrawSelection(char hiliteStat, const RunPositionPB* paramPb)
- {
- fProxyRun->SetSelectState(hiliteStat == kSelOn);
-
- if (hiliteStat == kSelOff)
- return;
-
- short selectionWidth = GetSelectionWidth();
-
- Rect objRect;
- this->GetObjectRect(paramPb, &objRect);
- InsetRect(&objRect, -selectionWidth, -selectionWidth);
-
- PenState savedPen;
- GetPenState(&savedPen);
-
- PenMode(patXor);
-
- ConstPatternParam pat = (hiliteStat == kSelInactiv) ? &ASLMQDGlobals.gray : &ASLMQDGlobals.black;
- PenPat(pat);
-
- FrameRect(&objRect);
-
- Rect handleRect;
- SetRect(&handleRect,
- objRect.right - selectionWidth - 1, objRect.bottom - selectionWidth - 1,
- objRect.right - 1, objRect.bottom - 1);
-
- InvertRect(&handleRect);
-
- SetPenState(&savedPen);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::DoClick
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::DoClick(ClickPB* clickPB, TClickCommandInfo* clickInfo, Boolean* abortDrag)
- {
- *abortDrag = true;
- if (this->PointInResizeHandle(clickPB))
- this->Resize(clickPB);
- else
- {
- CTextPart* textPart = (CTextPart *) fProxyRun->GetEmbeddingPart();
- XMPEventData event = textPart->GetEmbeddedRunEvent();
- CTextFacet* facet = textPart->GetEmbeddedRunFacet();
- textPart->GetSelection()->Drag(facet, event);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::PointInResizeHandle
- //----------------------------------------------------------------------------------------
-
- Boolean CEmbeddedRun::PointInResizeHandle(ClickPB* clickPB) const
- {
- CEmbeddedRun *self = (CEmbeddedRun *) this;
-
- Rect objRect;
- self->GetObjectRect(&clickPB->positionPB, &objRect);
-
- short selectionWidth = GetSelectionWidth();
-
- Rect handleRect;
- ::SetRect(&handleRect,
- objRect.right - selectionWidth - 1, objRect.bottom - selectionWidth - 1,
- objRect.right, objRect.bottom);
-
- return ::PtInRect(clickPB->clickPoint, &handleRect);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::Resize
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::Resize(ClickPB* clickPB)
- {
- CTextFacet *facet = ((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFacet();
-
- GrafPtr savedPort;
- ::GetPort(&savedPort);
- ::SetPort(facet->GetXMPWindow()->GetPlatformWindow());
-
- Point tmpPt = clickPB->clickPoint;
- ::LocalToGlobal(&tmpPt);
-
- if (!::WaitMouseMoved(tmpPt))
- return;
-
- Rect objRect;
- this->GetObjectRect(&clickPB->positionPB, &objRect);
- ::InsetRect(&objRect, -GetSelectionWidth(), -GetSelectionWidth());
-
- ::PenNormal();
- ::PenPat(&XMPASLMQDGlobals.gray);
- ::PenMode(patXor);
- ::PenSize(1, 1);
-
- Point curPt;
- Point lastPt = clickPB->clickPoint;
- while (::StillDown())
- {
- ::GetMouse(&curPt);
-
- this->DrawResizeFeedback(objRect);
-
- objRect.right += curPt.h - lastPt.h;
- objRect.bottom += curPt.v - lastPt.v;
-
- this->DrawResizeFeedback(objRect);
-
- lastPt = curPt;
- }
-
- ::PenNormal();
-
- ::SetPort(savedPort);
-
- this->ChangeShape(objRect);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::DrawResizeFeedback
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::DrawResizeFeedback(const Rect& rect)
- {
- ::FrameRect(&rect);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::AttributeToBuffer
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::AttributeToBuffer(AttrId theAttr, void* attrBuffer) const
- {
- if (theAttr == kProxyRunAttr)
- *longPtr(attrBuffer) = long(fProxyRun);
- else
- CBetterGraphicsRun::AttributeToBuffer(theAttr, attrBuffer);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::BufferToAttribute
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::BufferToAttribute(AttrId theAttr, const void* attrBuffer)
- {
- if (theAttr != kProxyRunAttr)
- CBetterGraphicsRun::BufferToAttribute(theAttr, attrBuffer);
- else
- fProxyRun = *((CTextProxyRun **) attrBuffer);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::GetAttrFlags
- //----------------------------------------------------------------------------------------
-
- long CEmbeddedRun::GetAttrFlags(AttrId theAttr, void* attrBuffer, long updateMessage /*=0*/) const
- {
- long flags = (theAttr == kProxyRunAttr) ? kReformat | kVReformat : 0;
-
- return flags | CBetterGraphicsRun::GetAttrFlags(theAttr, attrBuffer, updateMessage);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::GetDimensions
- //----------------------------------------------------------------------------------------
-
- Boolean CEmbeddedRun::GetDimensions(short* hite, short* width)
- {
- Rect rt;
- fProxyRun->GetBoundingBox(rt);
-
- *hite = rt.bottom - rt.top;
- *width = rt.right - rt.left;
-
- return true;
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::Draw
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::Draw(const Rect* theRect)
- {
- XMPShape *shape = ::NewXMPShape(*theRect);
-
- CTextFacet *facet = ((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFacet();
-
- XMPShape *invalidShape = ::NewXMPShape(*theRect);
- shape = ::NewXMPShape();
-
- FW_CEmbeddedXMPFacetsIterator ite(fProxyRun, facet ,kXMPFrontToBack);
- for (XMPFacet *embeddedXMPFacet = ite.First(); ite.IsNotComplete(); embeddedXMPFacet = ite.Next())
- {
- shape->CopyFrom(invalidShape);
- shape->Transform(facet->GetFrameTransform()); // Frame -> Window
- shape->InverseTransform(embeddedXMPFacet->GetFrameTransform()); // Window -> Frame
-
- embeddedXMPFacet->Draw(shape);
- embeddedXMPFacet->DrawChildren(shape);
- }
-
- facet->Validate(NULL);
-
- delete invalidShape;
- delete shape;
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::ChangePosition
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::ChangePosition(const Point& pt)
- {
- Rect rt;
- fProxyRun->GetBoundingBox(rt);
-
- if (rt.top == pt.v && rt.left == pt.h)
- return;
-
- short height = rt.bottom - rt.top;
- short width = rt.right - rt.left;
-
- rt.top = pt.v;
- rt.left = pt.h;
- rt.bottom = rt.top + height;
- rt.right = rt.left + width;
-
- fProxyRun->SetBoundingBox(rt);
-
- XMPShape *shape = ::NewXMPShape(rt);
-
- FW_CProxyFrame* proxy = fProxyRun->WhichProxyFrame(((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFrame());
-
- short svOrgX = XMPASLMQDGlobals.thePort->portRect.left;
- short svOrgY = XMPASLMQDGlobals.thePort->portRect.top;
-
- proxy->ResizeFrame(shape);
-
- ::SetOrigin(svOrgX, svOrgY);
- }
-
- //----------------------------------------------------------------------------------------
- // CEmbeddedRun::ChangeShape
- //----------------------------------------------------------------------------------------
-
- void CEmbeddedRun::ChangeShape(const Rect& rt)
- {
- fProxyRun->SetBoundingBox(rt);
-
- XMPShape *shape = ::NewXMPShape(rt);
-
- FW_CProxyFrame* proxy = fProxyRun->WhichProxyFrame(((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFrame());
-
- short svOrgX = XMPASLMQDGlobals.thePort->portRect.left;
- short svOrgY = XMPASLMQDGlobals.thePort->portRect.top;
-
- proxy->ResizeFrame(shape);
-
- ::SetOrigin(svOrgX, svOrgY);
-
- // ----- Reflow the text
-
- COpenDocText* textEdit = ((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEditText();
-
- CDisplayChanges displayChanges;
- displayChanges.SetChangesKind(kNeedVReformat | kNeedHReformat);
-
- short action = textEdit->DisplayChanged(&displayChanges);
-
- // ------ Notify embedded parts of position changes.
-
- textEdit->NotifyEmbeddedPartsOfPositionChange(0, textEdit->CountLines() - 1);
- }
-